import util from "../../utils/util";
import api from "../../utils/api";

Page({
    data: {
        MINIO: util.MINIO,
        pageInfo: {pageNumber: 1, pageSize: 8, totalPage: 0, totalRow: 0},
        follows: null,
    },

    // 分页查询收藏记录
    page: function () {
        let that = this;
        let pageNumber = that.data.pageInfo['pageNumber'];
        let pageSize = that.data.pageInfo['pageSize'];
        api.get('/member/pageFollowCourse', {
            'pageNumber': pageNumber,
            'pageSize': pageSize
        }).then(res => {
            that.setData({
                'follows': pageNumber === 1 ? res['records'] : that.data.carts.concat(res['records']),
                'pageInfo.pageNumber': res['pageNumber'],
                'pageInfo.pageSize': res['pageSize'],
                'pageInfo.totalPage': res['totalPage'],
                'pageInfo.totalRow': res['totalRow'],
            });
        }).catch(err => util.error('查询异常', err));
    },

    // 列表触底时追查下一页数据
    pageMore: function () {
        let that = this;
        let pageNumber = that.data.pageInfo['pageNumber'];
        let totalPage = that.data.pageInfo['totalPage'];
        if (pageNumber < totalPage) {
            this.setData({'pageInfo.pageNumber': this.data.pageInfo['pageNumber'] + 1});
            this.page();
        }
    },

    // 取消收藏课程
    unfollow: function (ev) {
        let id = ev.currentTarget.dataset['id'];
        util.confirm('选中的课程将被取消收藏，确定吗？', () => {
            api.post(`/member/unfollowCourse/${id}`).then(res => {
                util.success('取消收藏成功');
                this.page();
            }).catch(err => util.error('取消收藏失败', err));
        });
    },

    // 查看课程详情
    showDetail: function (ev) {
        let courseId = ev.currentTarget.dataset['courseId'];
        util.page('/pages/course/detail/detail?courseId=' + courseId, false);
    },

    // 加载函数
    onLoad: function (options) {
        if(util.isLogin()){
            this.page();
        }
    }
});